﻿using FS.Business;
using FS.Common.DotNetBean;
using FS.Common.DotNetUI;
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;

namespace FS.Web.BLL.Visa_Base
{
    public partial class Visa_infoSendForm : PageBase
    {
        private string _key;
        visa_dao _visa = new visa_dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            this._key = base.Request["key"];
            if (!base.IsPostBack)
            {
                //设置签证时间
                this.info_submitdate.Value = DateTime.Now.ToString("yyyy-MM-dd");

                if (!string.IsNullOrEmpty(this._key))
                {
                    this.InitData();
                    this.bind();
                }
            }
        }
                
        private void InitData()
        {
            Hashtable ht = _visa.GetVisaSendtoForm(this._key);
            if (ht.Count > 0 && ht != null)
            {               
                ControlBindHelper.SetWebControls(this.Page, ht);
            }
        }    

        protected void Save_Click(object sender, EventArgs e)
        {
            string ls_date = this.info_submitdate.Value;
            string ls_userid = RequestSession.GetSessionUser().UserId.ToString();
            string ls_username = RequestSession.GetSessionUser().UserName.ToString();
           
            bool rtn = _visa.UpdateDetailBatchSendDate(this._key, ls_date, ls_userid, ls_username);
            if (rtn )
            {
                ShowMsgHelper.AlertMsg("操作成功！");
            }
            else {
                ShowMsgHelper.Alert_Error("操作失败！");
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//1先判断当前行是不是数据行
            {
                if (e.Row.RowState == DataControlRowState.Edit ||
                  e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
                {
                    //对日期模板列添加单击事件WdatePicker()
                    e.Row.Cells[6].Attributes.Add("onclick", "WdatePicker({dateFmt: 'yyyy-MM-dd'});");

                    DropDownList ddl_data = e.Row.FindControl("ddl_data") as DropDownList;
                    string ls_data = ((HiddenField)e.Row.FindControl("Hd_data")).Value;

                    if (ddl_data != null)
                    {
                        ddl_data.Items.FindByValue(ls_data).Selected = true;                        
                    }
                }
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string ls_date = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_submitdate"))).Text;
            string infodetailid = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string ls_username = RequestSession.GetSessionUser().UserName.ToString();
            string ls_userid = RequestSession.GetSessionUser().UserId.ToString();
            string ls_flag = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("ddl_data"))).SelectedValue;

            bool isok = _visa.UpdateDetailSendDate(infodetailid, ls_date, ls_flag,ls_userid, ls_username);
            if (!isok)
            {
                ShowMsgHelper.Alert_Error("操作失败！");
            }
            else {
                GridView1.EditIndex = -1;
                bind();
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }

        public void bind()
        {
            DataTable dt = _visa.GetInfoDetail(this._key);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }
    }
}